<?php
namespace  app\manage\model;
use think\Db;
use think\Model;
class AdminUser extends Model{
    protected  $table = 'admin_user';
    /**
     * @param $page             页码
     * @param $rows             一页的列数
     * @param $keyword          关键字
     * @param $type             1表示平台管理员 2表示员工
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function onePage($page, $rows, $keyword, $type,$create_id='',  $department_id='', $type2='') {
        $where = [];
        if ($create_id != 1 && $create_id != '') {
            $where['create_id'] = $create_id;
        }
        if ($type == 1) {
            if ($type2 != '') {
                $where['type'] = $type2;
            }
            $data=Db::name($this->table)
                ->where('name|admin|admin_tel|address|street', 'like', '%'.$keyword.'%')
                ->where('admin_sign', 1)
                ->page($page, $rows)
                ->where('admin_status', '<>', 3)
                ->where($where)
                ->field('admin_id,name,type,admin,admin_tel,address,street,create_time,admin_status')
                ->order('create_time desc')
                ->select();
        } elseif ($type == 2) {
            if ($department_id != '') {
                $where['department_id'] = $department_id;
            }
            $data=Db::name($this->table)
                ->where('admin_sign', 2)
                ->where('admin_status', '<>', 3)
                ->page($page, $rows)
                ->where('name|job_number', 'like', '%'.$keyword.'%')
                ->where($where)
                ->field('admin_id,name,sex,admin_tel,department_id,department_name,role_id,role_name,job_number,admin_status')
                ->order('create_time desc')
                ->select();

        } else {
            returnJson(-2005,"未知账户类型")->send();die;
        }
        return $data;
    }

    /**
     * @param $keyword          关键字
     * @param $type             1表示平台管理员 2表示员工
     * @return int|string       总数
     * @throws \think\Exception
     */
    public function total($keyword, $type, $create_id='', $department_id='', $type2='') {
        $where = [];
        if ($create_id != 1 && $create_id != '') {
            $where['create_id'] = $create_id;
        }
        if ($type == 1) {
            if ($type2 != '') {
                $where['type'] = $type2;
            }
            $data=Db::name($this->table)
                ->where('name|admin|admin_tel|address|street', 'like', '%'.$keyword.'%')
                ->where('admin_sign', 1)
                ->where($where)
                ->where('admin_status', '<>', 3)
                ->count();
        } elseif ($type == 2) {
            if ($department_id != '') {
                $where['department_id'] = $department_id;
            }
            $data=Db::name($this->table)
                ->where('name|job_number', 'like', '%'.$keyword.'%')
                ->where('admin_sign', 2)
                ->where($where)
                ->where('admin_status', '<>', 3)
                ->count();

        } else {
            returnJson(-2005,"未知账户类型")->send();die;
        }
        if (gettype($data) !== 'integer') die;
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($admin_id, $data) {
        $data = Db::name($this->table)
            ->where(['admin_id'=>$admin_id])
            ->update($data);
        return $data;
    }

    /**
     * @param $admin_id
     * @param $type
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($admin_id, $type=0) {
        if ($type == 1) {
            $field = 'admin_id, name, type, address, street, admin, admin_tel, add_permission, admin_status';
        } elseif ($type == 2) {
            $field = 'admin_id, name, job_number, sex, admin_tel, department_id, department_name, role_id, role_name';
        } else {
            $field = '*';
        }
        $data = Db::name($this->table)
            ->where(['admin_id'=>$admin_id])
            ->field($field)
            ->find();
        return $data;
    }

    /**
     * @param $admin_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getKey($admin_id) {
        $data = Db::name($this->table)
            ->where(['admin_id'=>$admin_id])
            ->field('admin_id, key')
            ->find();
        return $data;
    }


    /**
     * @param $department_id
     * @return int|string
     * @throws \think\Exception
     */
    public function getDepartmentCount($department_id) {
        $data = Db::name($this->table)
            ->where(['department_id'=>$department_id])
            ->count();
        return $data;
    }

    /**
     * @param $role_id
     * @return int|string
     * @throws \think\Exception
     */
    public function getRoleCount($role_id) {
        $data = Db::name($this->table)
            ->where(['role_id'=>$role_id])
            ->count();
        return $data;
    }

    /**
     * @param $department_id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDataDepartment($department_id) {
        $data = Db::name($this->table)
            ->where(['department_id'=>$department_id])
            ->field('admin_id, name, job_number, admin_tel')
            ->select();
        return $data;
    }

    /**
     * 唯一性
     * @param $tel
     * @return int|string
     * @throws \think\Exception
     */
    public function uniqueness($tel, $create_id='') {
        $where = [];
        if ($create_id != '') {
            $where['create_id'] = $create_id;
        }
        $data = Db::name($this->table)
            ->where(['admin_tel'=>$tel])
            ->where($where)
            ->count();
        return $data;
    }

    /**
     * 找到对应的平台账户的id
     * @param $admin_id
     */
    public function findPlatId($admin_id) {
        $data = Db::name('admin_user')
            ->where('admin_id', $admin_id)
            ->where('admin_status', 1)
            ->find();
        if ($data['admin_sign'] == 1) {
            return $admin_id;
        } else {
            return $data['create_id'];
        }
    }

    /**
     * @param $type    1表示平台账户，2表示员工
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function m_list($type) {
        $data = Db::name($this->table)
            ->where('admin_sign', $type)
            ->where('admin_status', 1)
            ->select();
        return $data;
    }

    /**
     * @param $tel
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detailTel($tel) {
        $data = Db::name($this->table)
            ->where('admin_tel', $tel)
            ->find();
        return $data;
    }
}